package other.edan.erp.product;

import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEvent;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventRequest;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventResponse;
import com.rkhd.platform.sdk.data.model.Product;
import com.rkhd.platform.sdk.data.model.ProductModel__c;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import org.apache.commons.lang.StringUtils;
import other.edan.erp.utils.HCXObjectService;

/**
 * @Description: TODO
 * @Author: 黎炎松
 * @Date: 2024/12/11 17:56
 */
public class DealProductModel implements RuleEvent {

    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "新增、修改产品注册型号 ==> ";

    /**
     * 执行触发规则触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public RuleEventResponse execute(RuleEventRequest request) throws ScriptBusinessException {
        LOGGER.info(LOGGER_MSG + "执行触发事件");

        // 处理当前的出库
        dealProductModel(request.getDataId());

        RuleEventResponse response = new RuleEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }

    public static void main(String[] args) {
        DealProductModel productModel = new DealProductModel();
        productModel.dealProductModel(3380911276710486L);
    }

    /**
     * 处理产品型号
     * @param productId
     */
    public void dealProductModel(Long productId) {
        try {
            LOGGER.info(LOGGER_MSG + "开始处理");
            Product crmProduct = new Product();
            crmProduct.setId(productId);
            crmProduct = HCXObjectService.get(crmProduct);
            String fscProductModel = crmProduct.getFscProductModel();
            if (StringUtils.isBlank(fscProductModel)) {
                LOGGER.info(LOGGER_MSG + "产品型号为空，无需处理");
                return;
            }

            // 判断产品型号中是否存在
            String sql = String.format("select id from productModel__c where name = '%s'", fscProductModel);
            ProductModel__c crmData = HCXObjectService.queryOne(sql);

            // 重新赋值
            ProductModel__c productModel = new ProductModel__c();
            productModel.setProductLine__c(crmProduct.getProductLine__c());
            productModel.setProxyProducts__c(crmProduct.getProxyProducts__c());
            productModel.setProductSeries1__c(crmProduct.getProductSeries1__c());
            productModel.setProductSeries2__c(crmProduct.getProductSeries2__c());
            productModel.setShifouYiqi__c(crmProduct.getShifouYiqi__c());
            if (crmData == null) {
                LOGGER.info(LOGGER_MSG + "执行新增");
                productModel.setName(fscProductModel);
                productModel.setEntityType(3563548576242258L);
                HCXObjectService.insert(productModel);
            } else {
                LOGGER.info(LOGGER_MSG + "执行修改");
                productModel.setId(crmData.getId());
                HCXObjectService.update(productModel);
            }

            LOGGER.info(LOGGER_MSG + "处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(LOGGER_MSG + "处理异常");
        }
    }

}
